﻿using System;
using System.Windows.Forms;
using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;

namespace Ledict.ConditionExtract
{
    /// <summary>
    ///     添加关键词
    /// </summary>
    public partial class WB_AddKeyWords : BaseForm
    {
        private static WB_AddKeyWords _default;
        private static readonly object Lock = new object();
        //private Express_WordBankRelBLL express_wordbankrelbll = new Express_WordBankRelBLL();
        private readonly NewIDBLL newid = new NewIDBLL();
        public string WB_ID = "";
        public string WBK_ID = "";
        public string WBK_Word = "";
        private readonly WordBankKeyWordsBLL _wordbankkeywordsbll = new WordBankKeyWordsBLL();

        public WB_AddKeyWords()
        {
            InitializeComponent();
        }

        public static WB_AddKeyWords Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    lock (Lock)
                    {
                        if (_default == null || _default.IsDisposed)
                        {
                            _default = new WB_AddKeyWords();
                        }
                    }

                }
                return _default;
            }
        }

        /// <summary>
        /// 保存关键词
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                var model = new SYS_WordBank_KeyWords();
                if (WBK_ID == "")
                {
                    var wordsTextArr = WordsText.Text.Split(',', '，', ';', '；');
                    foreach (var item in wordsTextArr)
                    {
                        if (item != "")
                        {
                            model.WB_ID = WB_ID;
                            model.WBK_Word = item;
                            model.WBK_Length = item.Length;
                            model.WBK_ID = newid.JudgeDoc_CreateID("SYS_WordBank_KeyWords");
                            if (!_wordbankkeywordsbll.Exists(model))
                            {
                                if (_wordbankkeywordsbll.Add(model)) //添加
                                {
                                    MessageBox.Show("保存成功!");
                                }
                                // express_wordbankrelbll.AddList_Express_WordBankRel(model.WBK_ID, model.WBK_Word);
                            }
                        }
                    }
                }
                else
                {
                    model.WBK_ID = WBK_ID;
                    model.WBK_Word = WordsText.Text;
                    model.WBK_Length = WordsText.Text.Length;
                    if (_wordbankkeywordsbll.Update(model)) // 修改
                    {
                        MessageBox.Show("保存成功!");
                    }
                    // express_wordbankrelbll.Delete_Express_WordBankRel(model.WBK_ID);
                    // express_wordbankrelbll.AddList_Express_WordBankRel(model.WBK_ID, model.WBK_Word);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败!");
                Log.ErrorLog(ex.Message);
            }
            var wb_editwbkeywords = WB_EditWBKeyWords.Default;
            wb_editwbkeywords.WB_ID = WB_ID;
            wb_editwbkeywords.ShowGV_WB_AllWords();
            Close();
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void WB_AddKeyWords_Load(object sender, EventArgs e)
        {
            if (WBK_ID != "")
                WordsText.Text = WBK_Word;
        }
    }
}